<?php

namespace App\Http\Controllers;

use App\Http\Requests\IndexSignConfigRequest;
use App\Http\Requests\StoreSignConfigRequest;
use App\Http\Requests\UpdateSignConfigRequest;
use App\Http\Resources\SignConfigResource;
use App\Models\SignConfig;

class SignConfigController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth:sanctum');
    }

    /**
     * Display a listing of the resource.
     */
    public function index(IndexSignConfigRequest $request)
    {
        $signconfig = SignConfig::query();

        $signconfig->when($request->filled('start_time'), function ($query) use ($request) {
            $startTime = $request->input('start_time');
            return $query->where('start_time', $startTime);
        });

        $data = $signconfig->orderByDesc('created_at')->paginate(
            $request->query('pageSize'),
            ['*'],
            'page',
            $request->query('pageNum')
        );
        $response = SignConfigResource::collection($data);
        $total = $response->total(); // 获取分页信息中的总数
        return $this->success('查询成功', $response, $total);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(StoreSignConfigRequest $request, SignConfig $signConfig)
    {

        $startTime = $request->input('start_time');
        $endTime = $request->input('end_time');
        $exists = $signConfig->where('start_time', $startTime)->orWhere('end_time', $endTime)->exists();
        if ($exists) {
            return $this->error('周期范围已存在');
        }

        $signConfig->fill($request->input());
        $signConfig->saveOrFail();
        return $this->success('添加成功', $signConfig);
    }

    /**
     * Display the specified resource.
     */
    public function show(SignConfig $signConfig, $id)
    {
        $signConfig = $signConfig->findOrFail($id);
        $this->authorize('viewAny', $signConfig);
        $data = new SignConfigResource($signConfig);
        return $this->success('查询成功', $data);
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(SignConfig $signConfig)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(UpdateSignConfigRequest $request, SignConfig $signConfig, $id)
    {
        // 检查是否已存在相同的code
        $exists = SignConfig::where('start_time', $request->input('start_time'))
            ->where('end_time', $request->input('end_time'))
            ->where('id', '!=', $id) // 排除当前记录
            ->exists();
        if ($exists) {
            return $this->error('周期范围 已经存在，无法新增重复的 周期范围');
        }
        $signConfig = $signConfig->findOrFail($id);
        $signConfig->fill($request->input());
        $signConfig->saveOrFail();
        return $this->success('更新成功', $signConfig);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(SignConfig $signConfig, $id)
    {
        $signConfig = $signConfig->findOrFail($id);
        $this->authorize('delete', $signConfig);
        $signConfig->delete();
        return $this->success('删除成功', $signConfig);
    }
}
